package org.waigua.moka.processor;

import java.io.IOException;
import java.math.BigDecimal;
import java.util.Map;

import org.apache.commons.httpclient.HttpException;
import org.apache.log4j.Logger;
import org.waigua.moka.dto.ApiDto;
import org.waigua.moka.exception.BossHpException;
import org.waigua.moka.exception.SysException;
import org.waigua.moka.service.HttpApiService;
import org.waigua.moka.service.impl.HttpApiServiceImpl;
import org.waigua.moka.util.BigDecimalUtil;

public class BossProcessor
{
	protected Logger log = Logger.getLogger(getClass());
	HttpApiService httpApiService = new HttpApiServiceImpl();
	
	@SuppressWarnings("rawtypes")
	public boolean fightBoss() throws SysException
	{
		try
		{
			Map<String, ?> getBossMap = httpApiService.getBoss(ApiDto.buildApiDto());
			BigDecimal hp = BigDecimalUtil.convert(((Map)getBossMap.get("Boss")).get("BossCurrentHp"));
			if(hp.compareTo(BigDecimal.ZERO) <= 0)
			{
				throw new BossHpException();
			}
			BigDecimal canFightTime = BigDecimalUtil.convert(getBossMap.get("CanFightTime"));
			if(canFightTime.compareTo(BigDecimal.ZERO) != 0)
			{
				return false;
			}
			Map<String, ?> fightResultMap = httpApiService.fightBoss(ApiDto.buildApiDto());
			System.out.println(fightResultMap);
			return true;
		}
		catch (HttpException e)
		{
			throw new SysException(e);
		}
		catch (IOException e)
		{
			throw new SysException(e);
		}
	}
}
